//
//  File.cpp
//  http_server
//
//  Created by Zirpon Cheung on 15-3-25.
//  Copyright (c) 2015年 Zirpon Cheung. All rights reserved.
//

#include "File.h"


File::File(string szPath) : PATH(szPath)
{
    fp = fopen(PATH.c_str(), "r");//还没做安全检查 fp == NULL
    memset(buffer, '\0', 1024);
    bytes_read = fread(buffer, 1, sizeof (buffer), fp);
}

string File::getString(const string& szSearchKey, const string& def)
{
    if (bytes_read == 0 || bytes_read == sizeof (buffer))
        return def;
    
//    string szBuffer = buffer;
//    size_t b = szBuffer.find(szSearchKey);
//    string a = szBuffer.substr(b);
    
    char * match;
    
    match = strstr (buffer, szSearchKey.c_str());
    if (match == NULL)
        return def;
    char result[1024];
    memset(result, '\0', sizeof(result));
    
    string format = szSearchKey + "=%s";
    sscanf(match, format.c_str(), result);
    
    return result;
}

void File::close()
{
    fclose(fp);
}